using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Universal.Domain.Entities.App;

namespace Universal.Infrastructure.DB.Config;

public class AppRoleConfiguration : IEntityTypeConfiguration<AppRole>
{
    public void Configure(EntityTypeBuilder<AppRole> builder)
    {
        // 多对多：role_menus
        builder
            .HasMany(r => r.Menus)
            .WithMany(m => m.Roles)
            .UsingEntity<Dictionary<string, object>>(
                "role_menus",
                j => j.HasOne<AppMenu>().WithMany().HasForeignKey("menu_id"),
                j => j.HasOne<AppRole>().WithMany().HasForeignKey("role_id"),
                j =>
                {
                    j.HasKey("role_id", "menu_id");
                    j.ToTable("role_menus");
                });

        // 多对多：role_permissions
        builder
    .HasMany(r => r.Permissions)
    .WithMany(p => p.Roles)
    .UsingEntity<Dictionary<string, object>>(
        "role_permissions",
        j => j.HasOne<AppPermission>().WithMany().HasForeignKey("permission_id"),
        j => j.HasOne<AppRole>().WithMany().HasForeignKey("role_id"),
        j =>
        {
            j.HasKey("role_id", "permission_id");
            j.ToTable("role_permissions");
        });
    }
}
